package com.zhang.servicedriveruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.internalcommon.constant.CommonStatusEnum;
import com.zhang.internalcommon.constant.DriverCarConstant;
import com.zhang.internalcommon.dto.*;
import com.zhang.internalcommon.response.OrderDriverResponse;
import com.zhang.servicedriveruser.mapper.CarMapper;
import com.zhang.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.zhang.servicedriveruser.mapper.DriverUserMapper;
import com.zhang.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import com.zhang.servicedriveruser.service.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 类功能描述
 *
 * @author Kye
 * @date 2025-04-12
 */
@Service
public class DriverUserServiceImpl implements DriverUserService {
	@Autowired
	private DriverUserMapper driverUserMapper;

	@Autowired
	private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

	@Autowired
	private CarMapper carMapper;

	@Override
	public ResponseResult testGetDriverUser() {
		DriverUser driverUser = driverUserMapper.selectById(1);
		return ResponseResult.success(driverUser);
	}

	@Override
	public ResponseResult addDriverUser(DriverUser driverUser) {
		LocalDateTime now = LocalDateTime.now();
		driverUser.setGmtCreate(now);
		driverUser.setGmtModified(now);
		int insert = driverUserMapper.insert(driverUser);
		// 初始化 司机工作状态表
		DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
		driverUserWorkStatus.setDriverId(driverUser.getId());
		driverUserWorkStatus.setWorkStatus(DriverCarConstant.DRIVER_WORK_STATUS_STOP);
		driverUserWorkStatus.setGmtModified(now);
		driverUserWorkStatus.setGmtCreate(now);
		driverUserWorkStatusMapper.insert(driverUserWorkStatus);
		return ResponseResult.success();
	}

	@Override
	public ResponseResult updateDriverUser(DriverUser driverUser) {
		LocalDateTime now = LocalDateTime.now();
		driverUser.setGmtModified(now);
		driverUserMapper.updateById(driverUser);
		return ResponseResult.success();
	}

	@Override
	public ResponseResult getUser(String driverPhone) {
		HashMap<String, Object> map = new HashMap<>();
		map.put("driver_phone", driverPhone);
		map.put("state", DriverCarConstant.DRIVER_STATE_VALID);
		List<DriverUser> driverUsers = driverUserMapper.selectByMap(map);
		if (driverUsers.isEmpty()) {
			return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS.getCode(), CommonStatusEnum.DRIVER_NOT_EXISTS.getValue());
		}
		DriverUser driverUser = driverUsers.get(0);
		return ResponseResult.success(driverUser);
	}

	@Autowired
	private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

	@Override
	public ResponseResult<OrderDriverResponse> getAvailableDriver(Long carId) {
		// 车辆和司机绑定关系查询
		QueryWrapper<DriverCarBindingRelationship> driverCarBindingRelationshipQueryWrapper = new QueryWrapper<>();
		driverCarBindingRelationshipQueryWrapper.eq("car_id", carId);
		driverCarBindingRelationshipQueryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);

		DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(driverCarBindingRelationshipQueryWrapper);
		if (driverCarBindingRelationship == null) {
			return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(), CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
		}
		Long driverId = driverCarBindingRelationship.getDriverId();
		// 司机工作状态的查询
		QueryWrapper<DriverUserWorkStatus> driverUserWorkStatusQueryWrapper = new QueryWrapper<>();
		driverUserWorkStatusQueryWrapper.eq("driver_id", driverId);
		driverUserWorkStatusQueryWrapper.eq("work_status", DriverCarConstant.DRIVER_WORK_STATUS_START);

		DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(driverUserWorkStatusQueryWrapper);
		if (null == driverUserWorkStatus) {
			return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(), CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());

		} else {
			// 查询司机信息
			QueryWrapper<DriverUser> driverUserQueryWrapper = new QueryWrapper<>();
			driverUserQueryWrapper.eq("id", driverId);
			DriverUser driverUser = driverUserMapper.selectOne(driverUserQueryWrapper);
			// 查询车辆信息
			QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
			carQueryWrapper.eq("id", carId);
			Car car = carMapper.selectOne(carQueryWrapper);


			OrderDriverResponse orderDriverResponse = new OrderDriverResponse();
			orderDriverResponse.setCarId(carId);
			orderDriverResponse.setDriverId(driverId);
			orderDriverResponse.setDriverPhone(driverUser.getDriverPhone());

			orderDriverResponse.setLicenseId(driverUser.getLicenseId());
			orderDriverResponse.setVehicleNo(car.getVehicleNo());
			orderDriverResponse.setVehicleType(car.getVehicleType());

			return ResponseResult.success(orderDriverResponse);
		}
	}

	@Override
	public ResponseResult getDriverCarBindingRelationship(String phone) {
		QueryWrapper<DriverUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("driver_phone", phone);

		DriverUser driverUser = driverUserMapper.selectOne(queryWrapper);
		Long driverId = driverUser.getId();

		QueryWrapper<DriverCarBindingRelationship> driverCarBindingRelationshipQueryWrapper = new QueryWrapper<>();
		driverCarBindingRelationshipQueryWrapper.eq("driver_id", driverId);
		driverCarBindingRelationshipQueryWrapper.eq("bind_state", DriverCarConstant.DRIVER_CAR_BIND);

		DriverCarBindingRelationship relationship = driverCarBindingRelationshipMapper.selectOne(driverCarBindingRelationshipQueryWrapper);
		return ResponseResult.success(relationship);

	}
}
